package com.mxm.mirai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mxm.mirai.dao.Table_Json;
import com.mxm.mirai.dao.User;
import com.mxm.mirai.mapper.UserMapper;
import com.mxm.mirai.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 漫小猫
 * @since 2020-09-24
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired(required = false)
    UserMapper userMapper;
    @Override
    public String getPassword(String username){
        return  userMapper.getPassword(username);
    }
    @Override
    public Integer checkUserBanStatus(String username){
        return userMapper.checkUserBanStatus(username);
    }
    @Override
    public String getRole(String username){
        return userMapper.getRole(username);
    }

    @Override
    public Integer UpdatePassword(String username, String password) {
        return userMapper.UpdatePassword(username,password);
    }

    @Override
    public String getRolePermission(String username){
        return userMapper.getRolePermission(username);
    }
    @Override
    public String getPermission(String username){
        return userMapper.getPermission(username);
    }

    @Override
    public int getUserByUsernameCount(String username) {
        return userMapper.selectCount(new QueryWrapper<User>().eq("username",username));
    }
    @Override
    public User getUserByUsername(String username) {
        return userMapper.selectOne(new QueryWrapper<User>().eq("username",username));
    }
    @Override
    public String SelectOtherByUsername(String other) {
        return userMapper.SelectOtherByUsername(other);
    }

    @Override
    public Integer UpdateUsernameByOther(String username,String other) {
        return userMapper.UpdateUsernameByOther(username,other);
    }

    @Override
    public User SelectUserByUsername(String username) {
        return userMapper.selectOne(new QueryWrapper<User>().select("username","quota","registertime","duedate","ban").eq("username",username));
    }

    @Override
    public Integer AddUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public Integer DeleteUser(Integer id) {
        return userMapper.deleteById(id);
    }

    @Override
    public Integer UpdateUser(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public Integer UpdateUserBanInversion(Integer id) {
        return userMapper.UpdateUserBanInversion(id);
    }

    @Override
    public List<String> getUser() {
        return userMapper.getUser();
    }

    @Override
    public List<User> getUserS() {
        return userMapper.selectList(new QueryWrapper<User>().eq("role","user"));
    }

    @Override
    public Table_Json SelectUserListPage(String username, Integer current, Integer size) {
        //                              页数   几条数据
        Page<User> page = new Page<>(current,size);
        IPage<User> n = null;
        if(username!=null && !"".equals(username)){
             n = userMapper.selectPage(page,new QueryWrapper<User>().select("id","username","password", "permission","quota","money","registertime","duedate","other","ban").eq("role","user").like("username",username).orderByDesc("registertime"));
        }else {
            n = userMapper.selectPage(page,new QueryWrapper<User>().select("id","username","password", "permission","quota","money","registertime","duedate","other","ban").eq("role","user").orderByDesc("registertime"));
        }
        List<User> list = n.getRecords();
        Table_Json table_Json = new Table_Json();
        table_Json.setCode(0);
        table_Json.setCount(n.getTotal());
        table_Json.setData(list);
        return table_Json;
    }

    @Override
    public void openUser(String username) {
        userMapper.openUser(username);
    }

    @Override
    public void banUser(String username) {
        userMapper.banUser(username);
    }
}
